package com.block.datapush.special.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.block.common.enums.InfoTypeEnum;
import com.block.common.exception.NotifyExceptionService;
import com.block.datapush.model.dto.StarInfoDTO;
import com.block.datapush.special.service.IMatchUpdateStartLeveService;
import com.block.datapush.special.service.ISpecialCostomInfoService;
import com.block.datapush.special.service.ISpecialDataStarCountInfoService;
import com.block.db.entity.MemberExtInfo;
import com.block.db.entity.SpecialCostomInfo;
import com.block.db.entity.SpecialDataApiInfo;
import com.block.db.mapper.MemberExtInfoMapper;
import com.block.db.mapper.SpecialDataApiInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;

@Service
public class MatchUpdateStartLeveService implements IMatchUpdateStartLeveService {

    @Autowired
    NotifyExceptionService notifyExceptionService;
    @Autowired
    ISpecialCostomInfoService specialCostomInfoService;
    @Resource
    MemberExtInfoMapper memberExtInfoMapper;
    @Resource
    SpecialDataApiInfoMapper specialDataApiInfoMapper;
    @Autowired
    ISpecialDataStarCountInfoService specialDataStarCountInfoService;

    @Override
    public JSONObject update(Integer infoId, Map<String, String[]> parameterMap, String paramContent) {

        String message = StrUtil.format("【机构星级回调】请求数据展示【infoId】{}【parameterMap】{}【paramContent】{}",
                infoId, JSON.toJSONString(parameterMap), paramContent);

        JSONObject jsonObjectParamContent = JSONObject.parseObject(paramContent);
        if (ObjectUtil.isEmpty(jsonObjectParamContent)){
            JSONObject error = getError("传入的信息为空");
            return error;
        }

        String mobileMd5 = jsonObjectParamContent.getString("mobileMd5");
        Integer startLevel = jsonObjectParamContent.getInteger("startLevel");
        SpecialCostomInfo specialCostomInfo = specialCostomInfoService.getById(infoId);
        try {
            if (ObjectUtil.isEmpty(specialCostomInfo)) {
                JSONObject error = getError("infoId有误");
                return error;
            }

            if (ObjectUtil.equals(specialCostomInfo.getInfoType(), InfoTypeEnum.CRM.getValue())) {
                JSONObject error = getError("机构类型错误，该类型不可修改星级");
                return error;
            }

            if (StrUtil.isEmpty(mobileMd5)){
                JSONObject error = getError("传入的手机号MD5为空");
                return error;
            }

            if (startLevel == null){
                JSONObject error = getError("传入的星级有误");
                return error;
            }
            if (!isInRange(startLevel)) {
                JSONObject error = getError("该星级不在取值范围之内");
                return error;
            }

            MemberExtInfo memberExtInfo = memberExtInfoMapper.selectByMobileMd5(mobileMd5);
            if (ObjectUtil.isEmpty(memberExtInfo)){
                JSONObject error = getError("传入的手机号MD5有误");
                return error;
            }
            Long uid = memberExtInfo.getUid();
            Integer startLevelOld = memberExtInfo.getStartLevel();
            if (startLevelOld != -1){
                JSONObject error = getError("不可重复修改该用户星级");
                return error;
            }
            SpecialDataApiInfo specialDataApiInfo = specialDataApiInfoMapper.selectMerchantDataPushInfoByUidAndInfoId(infoId, uid);
            if (ObjectUtil.isEmpty(specialDataApiInfo)){
                JSONObject error = getError("该用户不可修改星级");
                return error;
            }

            memberExtInfoMapper.startLeverUpdate(uid, startLevel);

            // 统计机构回传的星级信息
            StarInfoDTO starInfoDTO = new StarInfoDTO();
            starInfoDTO.setInfoId(specialCostomInfo.getId());
            starInfoDTO.setInfoName(specialCostomInfo.getName());
            starInfoDTO.setUid(memberExtInfo.getUid());
            starInfoDTO.setStarLevel(startLevel);
            starInfoDTO.setMobileMd5(memberExtInfo.getMobileMd5());
            starInfoDTO.setChannelCode(memberExtInfo.getChannelCode());
            starInfoDTO.setChannelName(memberExtInfo.getChannelName());

            specialDataStarCountInfoService.saveStarCountInfo(starInfoDTO);

            return getSuccess("星级回传成功");
        }catch (Exception exception){
            exception.printStackTrace();
            notifyExceptionService.notifyNoticeInfo("STARTLEVEL_API_UPDATE", "修改星级异常", message, exception);
        }

        return getError("修改星级异常");
    }


    public JSONObject getError(String message) {
        JSONObject returnObject = new JSONObject();
        returnObject.put("code", 500);
        returnObject.put("msg", message);
        return returnObject;
    }

    public JSONObject getSuccess(String message) {
        JSONObject returnObject = new JSONObject();
        returnObject.put("code", 200);
        returnObject.put("msg", message);
        return returnObject;
    }

    public static boolean isInRange(int value) {
        return value >= 0 && value <= 5;
    }


}
